const Controller = require('../../core/controller/ApiController');
const {Route, HttpGet, HttpPost, HttpDelete, Middleware, filters} = require('../../../plugin/egg-decorator-router/lib');
const {ExceptionHandler} = require('../../../plugin/egg-controller-exception-handler/lib');

@Route('/api/admin/svcitem')
class SvcItemCtrl extends Controller {

    //搜索服务项目
    @HttpGet('/search')
    @ExceptionHandler()
    async searchItem() {
        const rule = {
            name: 'string'
        };
        this.validate(rule, this.ctx.request.query);
        this.result.data = await this.service.admin.svcitem.main.searchItem(this.ctx.request.query.name);
        return '搜索成功'
    }

    //编辑服务项目
    @HttpPost('/edit')
    @ExceptionHandler()
    async editItem() {
        const rule = {
            svc_item_id: 'int',
            new_name: 'string'
        };
        const Query = this.ctx.request.query;
        this.validate(rule, Query);
        await this.service.admin.svcitem.main.editItem(Query.svc_item_id, Query.new_name);
        return '修改成功'
    }

    //删除服务项目
    @HttpPost('/')
    @ExceptionHandler()
    async delete() {
        const rule = {
            svc_item_id: 'int'
        };
        this.validate(rule, this.ctx.request.body);
        await this.service.admin.svcitem.main.delete(this.ctx.request.body.svc_item_id);
        return '删除成功'
    }

    //添加服务项目
    @HttpPost('/add')
    @ExceptionHandler()
    async addItem() {
        const rule = {
            name: 'string'
        };
        this.validate(rule, this.ctx.request.query);
        await this.service.admin.svcitem.main.addItem(this.ctx.request.query.name);
        return '添加成功'
    }

    //服务项目查看统计
    @HttpGet('/:id/statistic')
    @ExceptionHandler()
    async viewStatistic() {
        const rule = {
            id: 'int'
        };
        this.validate(rule, this.ctx.params);
        this.result.data = await this.service.admin.svcitem.main.viewStatistic(this.ctx.params.id);
        return '查询成功'
    }
}

module.exports = SvcItemCtrl;
